﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
using ReflectionStudio.Core.Events;
using System.Windows.Controls;
namespace WPFExcelReport
{
    /// <summary>
    /// 运输公司与运输单号关系。
    /// </summary>
    public partial class TVV21
    {
        public TVV21()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VVVAVIS();
            this.DataContext = MV;
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            if (SART.Trim().Length==0)
                return;
            if (VSPED.Trim().Length==0)
                return;
            dwData = TBMain.NewRow();
            ((VVVAVIS)MV).SetUpNewRow(ref dwData);
            LFD = VSDDAT.SD22.GETNEXT("0022", "  ", "VVAVIS", 1);
            dwData["VVAAVISNR"] = Helper.Right("00000000" + LFD, 8);
            //dwData["KEY"] = Convert.ToDouble(LFD);
            dwData["VVASTATUS"] = " ";
            dwData["VVAVERSART"] = SART.PadRight(3);
            dwData["VVASPED"] = VSPED.PadRight(3);
            dwData["VVAFAHRER"] = FAHRER.PadRight(20); 
            
            Insert(dwData);
            vwMaster.View.MoveCurrentTo(-1);
            vwMaster.View.MoveCurrentToFirst();
            base.NewCommand(sender, e);
        }
        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            ((VVVAVIS)MV).UpdateVVAVIS(dwMaster, 0);
            base.SaveCommand(sender, e);
        }
        private void CloseCommand(object sender, RoutedEventArgs e)
        {
            dwMaster["VVASTATUS"] = "Y";
            ((VVVAVIS)MV).UpdateVVAVIS(dwMaster, 0);
            base.SaveCommand(sender, e);
        }
        public override void LOADBYAUF()
        {
            if (ORDERNUM.Length < 7)
                return;
            SART = ORDERNUM.Substring(0, 3);
            VSPED = ORDERNUM.Substring(3, 3);
            if (SART.Trim().Length == 0)
                return;
            if (VSPED.Trim().Length == 0)
                return;
            if (TMV.VVAAVISNR == "00000000")
            {
                TBMain.Clear();
                
                dwData = TBMain.NewRow();
                dwData["VVAMANDANT"] = app.Mandant;
                dwData["VVAVERSART"] = SART;
                dwData["VVASPED"] = VSPED;
                dwData["VVASTATUS"] = ORDERNUM.Substring(6, 1);
                SResult = ((VVVAVIS)MV).QueryVVAVIS(dwData, 3);
                MergeData(SResult, true);
                if (!IsAuto)
                    return;
                if (TBMain.Rows.Count > 0)
                {
                    if (LIEFSCH.Length == 8)
                    {

                    }
                    return;
                }
                NewCommand(null, null);
            }       
        }

        public override void OnSARTChg(string value)
        {
            base.OnSARTChg(value);
        }

        public override void OnVSPEDChg(string value)
        {
            base.OnSARTChg(value);
        }

        private void YD_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter)
                return;
            if (e.Key != System.Windows.Input.Key.Return)
                return;
            this.Focus();
            
            TextBox tb = sender as TextBox;
            NewCommand(null, null);
            tb.Focus();
        }
    }
}